package com.fanbo.springbootthymeleafblog.controller;

import com.fanbo.springbootthymeleafblog.entity.TArticle;
import com.fanbo.springbootthymeleafblog.service.BlogService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fanbo@imoran.net
 * @date 2021/1/17 16:40
 */
@Controller
@RequestMapping("/article")
public class LauncherController {

    private final int pageSize = 3;

    @Autowired
    private BlogService blogService;

    @RequestMapping("/index")
    public ModelAndView toLauncherPage(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<TArticle> articleList = blogService.pageList();
        PageInfo<TArticle> pageInfo = new PageInfo<>(articleList);
        Map data = new HashMap<>();//在返回的信息中选择性抽取
        data.put("total", pageInfo.getTotal());//总条数
        data.put("pages", pageInfo.getPages());//总页数
        data.put("current", pageNum);//当前页
        data.put("records", pageInfo.getList());//数据


        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("articlePage", data);
        modelAndView.setViewName("client/index");
        return modelAndView;
    }
}
